package com.thd.springboottest.springbootextends.custombeanpostprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * com.thd.springboottest.springbootextends.custombeanpostprocessor.MyBeanPostProcessor
 *
 * @author: wanglei62
 * @DATE: 2022/4/22 15:29
 **/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization >>>>>>>> :" + beanName + " " + bean.getClass());
        System.out.println("=======================  扩展 在SpringIOC中创建某个bean之前可以做扩展,例如创建一个代理类 =======================");
        if(bean.getClass()==ExampleBeanB.class){
            ExampleBeanC beanTransfer =  new ExampleBeanC();
            beanTransfer.setName("devil13th_C");
            return beanTransfer;
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization <<<<<<< :" + beanName + " " + bean.getClass());
        return null;
    }
}
